package tv.geir.slackspace.code;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * @author Geir Gullestad Pettersen
 *
 */
class ClassDefinitionRepository {

	private static Log log = LogFactory.getLog(ClassDefinitionRepository.class);
	
	private static ClassDefinitionRepository instance = null;
	
	private Map<String, ClassDefinition> definitions; 
	
	public ClassDefinitionRepository() {
		definitions = new HashMap<String, ClassDefinition>();
	}


	public ClassDefinition get(String key) {
		return definitions.get(key);
	}



	public ClassDefinition put( ClassDefinition value ) {
		ClassDefinition old = get( value.getClassName() );
		ClassDefinition stored = old;
		if( old != null && old.equals( value ) && log.isDebugEnabled() ) {
			log.debug( "Class definition alredy exist, ignoring" );
		}
		else if( old != null && !old.equals( value ) && log.isDebugEnabled() ) {
			log.debug( "Replacing class definition for " + value.getClassName() );
			stored = definitions.put( value.getClassName(), value );
		}
		else {
			stored = definitions.put( value.getClassName(), value );
		}
		return stored;
	}
}
